﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Windows.Forms;
using Util;
using System.Diagnostics;
using System.Windows.Media.Animation;
using System.Collections;
using System.IO;

namespace ISUM_V2
{
    /// <summary>
    /// Interaction logic for ScreenMouse.xaml
    /// </summary>
    public partial class ScreenMouse : Window, ICommunication
    {
        #region 멤버변수

        /// <summary>
        /// 휠 값 변경폭은 120
        /// </summary>

        private int _X { get; set; }
        private int _Y { get; set; }
        private int _ScreenSizeY { get; set; }
        private int _ScreenSizeX { get; set; }
        public int _MoveValue { get; set; }
        public uint _WheelUp { get; set; }
        public uint _WheelDown { get; set; }
        private bool _IsOption { get; set; }
        private bool _IsHide { get; set; }
        private bool _IsDevide { get; set; }
        private string _RecycleBuffer { get; set; }
        private string _Animation;
        private DevideWindow _DevideWindow;
        private MainWindow _MainWindow { get; set; }

        private ArrayList _CustomSignal { get; set; }
        private ArrayList CurrentImage { get; set; }
        //private CustomKeyboard _CustomKeyboard { get; set; }

        private MorseCodeFileModule _Mcfm { get; set; }
        private SrzMouseOption _SrzMouseOption { get; set; }
        private SrzDefaultOption _DefaultOption { get; set; }

        /// <summary>
        /// 현재 윈도우의 크기 상태
        /// </summary>
        private _WindowSizeType _CurrentWindowSize { get; set; }

        /// <summary>
        /// 마우스 윈도우의 크기로 Small은 0.8배, Normal은 기본인 1배, Large는 1.5배의 크기
        /// </summary>
        public enum _WindowSizeType
        {
            Small,
            Normal,
            Large
        }

        public enum _CustomSignalType : int
        {
            BackMain,
            Devide,
            Down,
            Drag,
            LeftClick,
            LeftDoubleClick,
            LeftDown,
            Left,
            LeftRightClick,
            LeftUp,
            Option,
            Recycle,
            RightClick,
            RightDoubleClick,
            RightDown,
            RightUp,
            Up,
            WheelDown,
            WheelUp,
            ExpolorerBack,
            ExpolorerNext,
            ExpolorerClose,
            ExplorerStart,
            ExplorerChangeSize,
            ExplorerSizeUp,
            ExplorerSizeDown,
            ExplorerSizeOrigin,
            ExplorerSetting,
            ExplorerFavorites,
            ExplorerAddress,
            MouseEventChangeWindowSize
			
        }

        public delegate void BluetoothKeyEvent ();
        private BluetoothKeyEvent[] _BluetoothKeyEvents = null;

        #endregion

        #region 생성자

        public ScreenMouse ( MainWindow main )
        {
            InitializeComponent();
            InitBluetoothKeyEvent();
            InitCustomSignal();
            InitializeVariable();
            SetWindowLocation();
            setEventRectDragMove();
            _MainWindow = main;
            this.Closed += new EventHandler( ScreenMouse_Closed );
            InitMorseImage();
            LoadDefaultOption();
            ToggleWindowHideShow();
        }

        void ScreenMouse_Closed ( object sender, EventArgs e )
        {
            _MainWindow.MainWindow_Closed( sender, e );
        }

        #endregion

        public void InitMorseImage ()
        {
            AddCurrentImage(); // 이미지 다담기

            _Mcfm = MorseCodeFileModule.getInstance(); // 파일모듈 인스턴스 불러오고

            SrzMouse srzMouse = (SrzMouse)_Mcfm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/Mouse.data"); // 역직렬화

            Image tempImage = null; //  템프 이미지 변수

            for ( int i = 0; i < CurrentImage.Count; i++ )
            {
                tempImage = (Image)CurrentImage[ i ];
                tempImage.Source = new BitmapImage( new Uri( (string)srzMouse.ArrMouseCustomMorseCodeSrc[ i ] ) );
            }

            SetMouseOption();
        }

        public void LoadDefaultOption()
        {

            _Mcfm = MorseCodeFileModule.getInstance();


            string pathDefaultOption = MainWindow.GetDoubleParent() + "/SrzMoreseCode/DefaultOption.data";
            if (File.Exists(pathDefaultOption))
            {
                _DefaultOption = (SrzDefaultOption)_Mcfm.readFile(pathDefaultOption);
            }
            else
            {
                _DefaultOption = new SrzDefaultOption();
            }

            _IsHide = !_DefaultOption.IsAutoHidden;
            
            //UpdateWindowHideShow();
        }

        private void UpdateWindowHideShow()
        {
            _IsHide = _DefaultOption.IsAutoHidden;
            if (!_DefaultOption.IsAutoHidden)
            {
                StartShow_StoryBoard();
            }
            else
            {
                
                StartHide_StoryBoard();
                
            }
        }

        public void SetMouseOption()
        {
            _Mcfm = MorseCodeFileModule.getInstance(); // 파일모듈 인스턴스 불러오고

            string pathMouseOption = MainWindow.GetDoubleParent() + "/SrzMoreseCode/MouseOption.data";
            if (File.Exists(pathMouseOption))// && _Mcfm.readFile(pathMouseOption) is SrzMouseOption)
            {
                _SrzMouseOption = (SrzMouseOption)_Mcfm.readFile(pathMouseOption); 
                this._MoveValue = _SrzMouseOption.CurrentMoveRage * 5;
                this._WheelUp = (uint)_SrzMouseOption.CurrentWheelRage * 120;
                this._WheelDown = (uint)(4294967055 - (_SrzMouseOption.CurrentWheelRage * 120));

                this.tbMouseMoveValue.Text = _SrzMouseOption.CurrentMoveRage.ToString();
                this.tbMouseWheelValue.Text = _SrzMouseOption.CurrentWheelRage.ToString();
            }




            if (_SrzMouseOption.IsCheckMaxSize == 0)
                _CurrentWindowSize = _WindowSizeType.Large;
            else if (_SrzMouseOption.IsCheckMaxSize == 1)
                _CurrentWindowSize = _WindowSizeType.Small;
            else if (_SrzMouseOption.IsCheckMaxSize == 2)
                _CurrentWindowSize = _WindowSizeType.Normal;

            ChangeWindowSize();
       

        }

        //public void SetMouseOption(string moveValue, string wheelmoveValue)
        //{
        //    _Mcfm = MorseCodeFileModule.getInstance(); // 파일모듈 인스턴스 불러오고

        //    string pathKeyboardOption = MainWindow.GetDoubleParent() + "/SrzMoreseCode/MouseOption.data";
        //    if (File.Exists(pathKeyboardOption))
        //    {
        //        _SrzMouseOption = (SrzMouseOption)_Mcfm.readFile(pathKeyboardOption);
        //        this._MoveValue = _SrzMouseOption.CurrentMoveRage * 5;
        //        this._WheelUp = (uint)_SrzMouseOption.CurrentWheelRage * 120;
        //        this._WheelDown = (uint)(4294967055 - (_SrzMouseOption.CurrentWheelRage * 120));

        //        this.tbMouseMoveValue.Text = _SrzMouseOption.CurrentMoveRage.ToString();
        //        this.tbMouseWheelValue.Text = _SrzMouseOption.CurrentWheelRage.ToString();
        //    }
        //}

        private void AddCurrentImage ()
        {
            CurrentImage = new ArrayList();

            CurrentImage.Add( imgBackMainMouse );
            CurrentImage.Add( imgDevideMouse );
            CurrentImage.Add( imgDownMouse );
            CurrentImage.Add( imgDragMouse );
            CurrentImage.Add( imgLeftClickMouse );
            CurrentImage.Add( imgLeftDoubleClickMouse );
            CurrentImage.Add( imgLeftDownMouse );
            CurrentImage.Add( imgLeftMouse );
            CurrentImage.Add( imgLeftRightClickMouse );
            CurrentImage.Add( imgLeftUpMouse );
            CurrentImage.Add( imgOptionMouse );
            CurrentImage.Add( imgRecycleMouse );
            CurrentImage.Add( imgRightClickMouse );
            CurrentImage.Add( imgRightDoubleClickMouse );
            CurrentImage.Add( imgRightDownMouse );
            CurrentImage.Add( imgRightMouse );
            CurrentImage.Add( imgRightUpMouse );
            CurrentImage.Add( imgUpMouse );
            CurrentImage.Add( imgWheelDownMouse );
            CurrentImage.Add( imgWheelUpMouse );
            CurrentImage.Add( imgMorseOption );
            CurrentImage.Add( imgMorseOption );

            CurrentImage.Add(imgExpAdressMouse);
            CurrentImage.Add(imgExpBackMouse);
            CurrentImage.Add(imgExpColseMouse);
            CurrentImage.Add(imgExpDecreaseMouse);
            CurrentImage.Add(imgExpFavoriteMouse);
            CurrentImage.Add(imgExpIncreaseMouse);
            CurrentImage.Add(imgExpNewExpMouse);
            CurrentImage.Add(imgExpNextMouse);
            CurrentImage.Add(imgExpOptionMouse);
            CurrentImage.Add(imgExpRefleshMouse);
            CurrentImage.Add(imgExpStartMouse);
            CurrentImage.Add(imgChangeWindowSize);
            
        }

        public void InitCustomSignal ()
        {
            _Mcfm = MorseCodeFileModule.getInstance();
            SrzMouse srzMouse = (SrzMouse)_Mcfm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/Mouse.data");
            this._CustomSignal = srzMouse.ArrMouseMorseCode;
        }

        private void InitBluetoothKeyEvent ()
        {
            _BluetoothKeyEvents = new BluetoothKeyEvent[]{
                new BluetoothKeyEvent(MouseEventBackMain),
                new BluetoothKeyEvent(MouseEventDevideDelegate),
                new BluetoothKeyEvent(MouseEventDown),
                new BluetoothKeyEvent(MouseEventDrag),
                new BluetoothKeyEvent(MouseEventLeftClick),
                new BluetoothKeyEvent(MouseEventLeftDoubleClick),
                new BluetoothKeyEvent(MouseEventLeftDown),
                new BluetoothKeyEvent(MouseEventLeft),
                new BluetoothKeyEvent(MouseEventLeftRightClick),
                new BluetoothKeyEvent(MouseEventLeftUp),
                new BluetoothKeyEvent(MouseEventOption),
                new BluetoothKeyEvent(MouseEventRecycleCenter),
                new BluetoothKeyEvent(MouseEventRightClick),
                new BluetoothKeyEvent(MouseEventRightDoubleClick),
                new BluetoothKeyEvent(MouseEventRightDown),
                new BluetoothKeyEvent(MouseEventRight),
                new BluetoothKeyEvent(MouseEventRightUp),
                new BluetoothKeyEvent(MouseEventUp),
                new BluetoothKeyEvent(MouseEventWheelDown),
                new BluetoothKeyEvent(MouseEventWheelUp),
                new BluetoothKeyEvent(MouseEventToggleHideShow),
                new BluetoothKeyEvent(KeyEventShowOption),

                new BluetoothKeyEvent(ExplorerAddress),
                new BluetoothKeyEvent(ExplorerBack),
                new BluetoothKeyEvent(ExplorerClose),
                new BluetoothKeyEvent(ExplorerSizeDown),
                new BluetoothKeyEvent(ExplorerFavorites),
                new BluetoothKeyEvent(ExplorerSizeUp),
                new BluetoothKeyEvent(ExplorerChangeSize),
                new BluetoothKeyEvent(ExplorerNext),
                new BluetoothKeyEvent(ExplorerSetting),
                new BluetoothKeyEvent(ExplorerSizeOrigin),
                new BluetoothKeyEvent(ExplorerStart),
                new BluetoothKeyEvent(MouseEventChangeWindowSize)
                
            };
        }
        
        #region 멤버변수 초기화

        private void InitializeVariable ()
        {
            _X = 0;
            _Y = 0;
            SetScreenXY();
            //_WheelUp = 360;
            //_WheelDown = 4294967055;
            //_MoveValue = 20;
            _IsOption = false;
            _IsHide = false;
            _IsDevide = false;
            _DevideWindow = null;
            _RecycleBuffer = "";
            MouseValueRefresh();
        }

        private void SetScreenXY ()
        {
            _ScreenSizeX = (int)SystemParameters.PrimaryScreenWidth;
            _ScreenSizeY = (int)SystemParameters.PrimaryScreenHeight;
        }

        private void MouseValueRefresh ()
        {
            tbMouseMoveValue.Text = ( _MoveValue / 5 ).ToString();
            tbMouseWheelValue.Text = ( _WheelUp / 120 ).ToString();
        }

        #endregion

        #region 마우스폼 드래그 이동
        /// <summary>
        /// 마우스 폼 드래그 이동
        /// </summary>
        void setEventRectDragMove ()
        {
            rectBackground.MouseLeftButtonDown += new MouseButtonEventHandler( rectBackground_MouseLeftButtonDown );
        }

        void rectBackground_MouseLeftButtonDown ( object sender, MouseButtonEventArgs e )
        {
            if ( this.WindowState == WindowState.Maximized )
            {
                this.WindowState = WindowState.Normal;
                this.Left = e.GetPosition( null ).X - 30;
                this.Top = e.GetPosition( null ).Y - 20;

                DragMove();
            }
            else
            {
                this.DragMove();
            }
        }
        #endregion

        #region 마우스폼 스타팅 포인트
        /// <summary>
        /// 마우스 폼 스타팅 포인트
        /// </summary>
        private void SetWindowLocation ()
        {
            this.Left = 0;
            this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
        }
        #endregion

        #region 마우스 현재 위치 얻기
        /// <summary>
        /// Winapi 에 마우스 위치 얻기 관련 구조체와 dll 추가했음
        /// </summary>
        public void GetMousePosition ()
        {
            WinApi.POINT mouse;
            WinApi.GetCursorPos( out mouse );
            this._X = mouse.x;
            this._Y = mouse.y;
        }
        #endregion

        #region 마우스 스토리보드

        private delegate void DelegateStartStoryboardMouse ( System.Windows.Controls.Button btn );
        private delegate void MouseMoveAnimation ( string s );

        private void InvokeMethodStartStoryboardMouse ( System.Windows.Controls.Button btn )
        {
            btn.TabIndex = 99;
            btn.TabIndex = 100;
        }

        private void StartStoryboardMouse ( System.Windows.Controls.Button btn )
        {
            btn.Dispatcher.BeginInvoke( new DelegateStartStoryboardMouse( InvokeMethodStartStoryboardMouse ), btn );
        }

        private void MouseAnimation ( string where )
        {
            Storyboard str;
            str = (Storyboard)this.FindResource( where );
            str.Begin();
        }

        private void MouseEventToggleHideShow ()
        {
            this.Dispatcher.BeginInvoke(new NoParamDelegate(ToggleWindowHideShow));
        }

        private void ToggleWindowHideShow ()
        {
            if ( _IsHide )
            {
                StartShow_StoryBoard();
            }
            else
            {
                _CurrentWindowSize = _WindowSizeType.Small;
                ChangeWindowSize();
                StartHide_StoryBoard();
            }
        }

        public void StartShow_StoryBoard ()
        {
            _IsHide = false;
            SetWindowLocation();
            var sb = ( (Storyboard)this.FindResource( "Storyboard_Show" ) );
            sb.Begin( this, true );
        }

        public void StartHide_StoryBoard ()
        {
            _IsHide = true;
            SetWindowLocation();
            var sb = ( (Storyboard)this.FindResource( "Storyboard_Hide" ) );
            sb.Begin( this, true );
        }

        #endregion

        #region 마우스 이벤트



        private void KeyEventCombineMacro ( WinApi.MacroKeys mk1, WinApi.MacroKeys mk2 )
        {
            KeyEventMacroDown( mk1 );
            KeyEventMacroDown( mk2 );

            KeyEventMacroUp( mk2 );
            KeyEventMacroUp( mk1 );                       
        }


        private static void KeyEventMacroDown ( WinApi.MacroKeys mk )
        {
            WinApi.keybd_event( (byte)mk, 0, 0, 0 );
        }

        private static void KeyEventMacroUp ( WinApi.MacroKeys mk )
        {
            WinApi.keybd_event( (byte)mk, 0, 2, 0 );
        }

        private delegate void DevideMoveToEnd ( int x, int y );

        /// <summary>
        /// 마우스 이동에 따른 이벤트
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        private void MouseEventMove ( string where )
        {
            SetScreenXY();
            switch ( where )
            {
                case "LeftUp":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6, _ScreenSizeY / 6 );
                    }
                    else
                    {
                        MouseEventValueMove( -_MoveValue, -_MoveValue );
                    }
                    //_Animation = "StrMouseLeftUp";
                    break;

                case "Up":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6 * 3, _ScreenSizeY / 6 );
                    }
                    else
                    {
                        MouseEventValueMove( 0, -_MoveValue );
                    }
                    //_Animation = "StrMouseUp";
                    break;

                case "RightUp":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6 * 5, _ScreenSizeY / 6 );
                    }
                    else
                    {
                        MouseEventValueMove( _MoveValue, -_MoveValue );
                    }
                    //_Animation = "StrMouseRightUp";
                    break;

                case "Left":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6, _ScreenSizeY / 6 * 3 );
                    }
                    else
                    {
                        MouseEventValueMove( -_MoveValue, 0 );
                    }
                    //_Animation = "StrMouseLeft";
                    break;

                case "Center":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6 * 3, _ScreenSizeY / 6 * 3 );
                    }
                    break;

                case "Right":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6 * 5, _ScreenSizeY / 6 * 3 );
                    }
                    else
                    {
                        MouseEventValueMove( _MoveValue, 0 );
                    }
                    //_Animation = "StrMouseRight";
                    break;

                case "LeftDown":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6, _ScreenSizeY / 6 * 5 );
                    }
                    else
                    {
                        MouseEventValueMove( -_MoveValue, _MoveValue );
                    }
                    //_Animation = "StrMouseLeftDown";
                    break;

                case "Down":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6 * 3, _ScreenSizeY / 6 * 5 );
                    }
                    else
                    {
                        MouseEventValueMove( 0, _MoveValue );
                    }
                    //_Animation = "StrMouseDown";
                    break;

                case "RightDown":
                    if ( _IsDevide )
                    {
                        this.Dispatcher.BeginInvoke( new DevideMoveToEnd( MouseEventDevideMove ), _ScreenSizeX / 6 * 5, _ScreenSizeY / 6 * 5 );
                    }
                    else
                    {
                        MouseEventValueMove( _MoveValue, _MoveValue );
                    }
                    //_Animation = "StrMouseRightDown";
                    break;
            }

            //if ( !where.Equals( "Center" ) )
            //{
            //    this.Dispatcher.BeginInvoke( new MouseMoveAnimation( MouseAnimation ), _Animation );
            //}

            WinApi.SetCursorPos( this._X, this._Y );
        }

        private void MouseEventDevideMove ( int x, int y )
        {
            GetMousePosition();

            this._X = x;
            this._Y = y;

            WinApi.SetCursorPos( _X, _Y );

            _IsDevide = false;
            _DevideWindow.Close();
            _DevideWindow = null;
        }

        private void MouseEventValueMove ( int x, int y )
        {
            GetMousePosition();

            this._X += x;
            this._Y += y;

            WinApi.SetCursorPos( _X, _Y );
        }

        public void MouseEventSingle ( WinApi.VirtualMouse vmdown, WinApi.VirtualMouse vmup )
        {
            WinApi.mouse_event( (uint)vmdown, 0, 0, 0, 0 );
            WinApi.mouse_event( (uint)vmup, 0, 0, 0, 0 );
        }

        public void MouseEventWheel ( WinApi.VirtualMouse vm, uint move )
        {
            WinApi.mouse_event( (uint)WinApi.VirtualMouse.WHEEL, 0, 0, move, 0 );
        }

        private void MouseEventOption ()
        {
            _IsOption = !_IsOption;

            this.Dispatcher.BeginInvoke( new NoParamDelegate( MouseButtonImageChange ) );

            StartStoryboardMouse( Btn_Mouse_Option );
        }

        private void MouseEventBackMain ()
        {
            _MainWindow.KillProcess();
        }

        private void MouseEventChangeKeyboard ()
        {
        }

        private void MouseEventRecycle ()
        {
            if ( !_RecycleBuffer.Equals( String.Empty ) )
            {
                WriteBluetoothSignal( _RecycleBuffer );
            }

            StartStoryboardMouse( Btn_Recycle );
        }

        private void MouseEventLeftUp ()
        {
            MouseEventMove( "LeftUp" );

            StartStoryboardMouse( Btn_LeftUp );
        }

        private void MouseEventUp ()
        {
            MouseEventMove( "Up" );

            StartStoryboardMouse( Btn_Up );
        }

        private void MouseEventRightUp ()
        {
            MouseEventMove( "RightUp" );

            StartStoryboardMouse( Btn_RightUp );
        }

        private void MouseEventLeft ()
        {
            MouseEventMove( "Left" );

            StartStoryboardMouse( Btn_Left );
        }

        private void MouseEventCenter ()
        {
            MouseEventMove( "Center" );

            StartStoryboardMouse( Btn_Recycle );
        }

        private void MouseEventRight ()
        {
            MouseEventMove( "Right" );

            StartStoryboardMouse( Btn_Right );
        }

        private void MouseEventLeftDown ()
        {
            MouseEventMove( "LeftDown" );

            StartStoryboardMouse( Btn_LeftDown );
        }

        private void MouseEventDown ()
        {
            MouseEventMove( "Down" );

            StartStoryboardMouse( Btn_Down );
        }

        private void MouseEventRightDown ()
        {
            MouseEventMove( "RightDown" );

            StartStoryboardMouse( Btn_RightDown );
        }

        private void MouseEventDrag ()
        {
            MouseEventSingle( WinApi.VirtualMouse.LEFT_DOWN, 0 );

            StartStoryboardMouse( Btn_Drag );
        }

        private void MouseEventLeftClick ()
        {
            MouseEventSingle( WinApi.VirtualMouse.LEFT_DOWN, WinApi.VirtualMouse.LEFT_UP );

            StartStoryboardMouse( Btn_LeftClick );
        }

        private void MouseEventLeftDoubleClick ()
        {
            MouseEventSingle( WinApi.VirtualMouse.LEFT_DOWN, WinApi.VirtualMouse.LEFT_UP );
            MouseEventSingle( WinApi.VirtualMouse.LEFT_DOWN, WinApi.VirtualMouse.LEFT_UP );

            StartStoryboardMouse( Btn_LeftDoubleClick );
        }

        private void MouseEventRightClick ()
        {
            if ( _IsOption )
            {
                if (_MoveValue > 5)
                {
                    _MoveValue -= 5;
                    this.Dispatcher.BeginInvoke(new NoParamDelegate(MouseValueRefresh));
                }
            }

            else
            {
                MouseEventSingle( WinApi.VirtualMouse.RIGHT_DOWN, WinApi.VirtualMouse.RIGHT_UP );
            }

            StartStoryboardMouse( Btn_RightClick );
        }

        private void MouseEventRightDoubleClick ()
        {
            if ( _IsOption )
            {
                if (_MoveValue < 100)
                {
                    _MoveValue += 5;
                    this.Dispatcher.BeginInvoke(new NoParamDelegate(MouseValueRefresh));
                }
            }

            else
            {
                MouseEventSingle( WinApi.VirtualMouse.RIGHT_DOWN, WinApi.VirtualMouse.RIGHT_UP );
                MouseEventSingle( WinApi.VirtualMouse.RIGHT_DOWN, WinApi.VirtualMouse.RIGHT_UP );
            }

            StartStoryboardMouse( Btn_RightDoubleClick );
        }

        private void MouseEventWheelUp ()
        {
            if ( _IsOption )
            {
                if (_WheelUp < 600)
                {
                    _WheelUp += 120;
                    _WheelDown -= 120;
                }
            }

            else
            {
                MouseEventWheel( WinApi.VirtualMouse.WHEEL, _WheelUp );
            }
            this.Dispatcher.BeginInvoke( new NoParamDelegate( MouseValueRefresh ) );
            StartStoryboardMouse( Btn_WheelUp );
        }

        private void MouseEventWheelDown ()
        {
            if ( _IsOption )
            {
                if (_WheelUp > 120)
                {
                    _WheelUp -= 120;
                    _WheelDown += 120;
                }
            }

            else
            {
                MouseEventWheel( WinApi.VirtualMouse.WHEEL, _WheelDown );
            }
            this.Dispatcher.BeginInvoke( new NoParamDelegate( MouseValueRefresh ) );
            StartStoryboardMouse( Btn_WheelDown );
        }

        private void MouseEventLeftRightClick ()
        {
            MouseEventSingle( WinApi.VirtualMouse.LEFT_DOWN, WinApi.VirtualMouse.LEFT_UP );
            MouseEventSingle( WinApi.VirtualMouse.RIGHT_DOWN, WinApi.VirtualMouse.RIGHT_UP );

            StartStoryboardMouse( Btn_LeftRightClick );
        }

        public delegate void NoParamDelegate ();

        private void MouseButtonImageChange ()
        {
            if ( _IsOption )
            {
                imgMouseWheelUp.Visibility = Visibility.Hidden;
                imgMouseWheelDown.Visibility = Visibility.Hidden;
                imgMouseRightClick.Visibility = Visibility.Hidden;
                //Grid_MouseLeftDouble.Visibility = Visibility.Hidden;
                Grid_MouseRightDouble.Visibility = Visibility.Hidden;

                imgMouseWheelValuePlus.Visibility = Visibility.Visible;
                imgMouseWheelValueMinus.Visibility = Visibility.Visible;
                imgMouseMoveValuePlus.Visibility = Visibility.Visible;
                imgMouseMoveValueMinus.Visibility = Visibility.Visible;

            }

            else
            {
                imgMouseWheelUp.Visibility = Visibility.Visible;
                imgMouseWheelDown.Visibility = Visibility.Visible;
                imgMouseRightClick.Visibility = Visibility.Visible;
                //Grid_MouseLeftDouble.Visibility = Visibility.Visible;
                Grid_MouseRightDouble.Visibility = Visibility.Visible;

                imgMouseWheelValuePlus.Visibility = Visibility.Hidden;
                imgMouseWheelValueMinus.Visibility = Visibility.Hidden;
                imgMouseMoveValuePlus.Visibility = Visibility.Hidden;
                imgMouseMoveValueMinus.Visibility = Visibility.Hidden;
            }
        }

        private void MouseEventDevide ()
        {
            SetScreenXY();
            if ( _DevideWindow == null )
            {
                _DevideWindow = new DevideWindow();

                _IsDevide = true;

                Grid draw = new Grid();

                Polyline Parent_x_1 = new Polyline();
                Parent_x_1.Stroke = new SolidColorBrush( Colors.MidnightBlue );
                Parent_x_1.StrokeThickness = 3.0;

                Polyline Parent_x_2 = new Polyline();
                Parent_x_2.Stroke = new SolidColorBrush( Colors.MidnightBlue );
                Parent_x_2.StrokeThickness = 3.0;

                Polyline Parent_y_1 = new Polyline();
                Parent_y_1.Stroke = new SolidColorBrush( Colors.MidnightBlue );
                Parent_y_1.StrokeThickness = 3.0;

                Polyline Parent_y_2 = new Polyline();
                Parent_y_2.Stroke = new SolidColorBrush( Colors.MidnightBlue );
                Parent_y_2.StrokeThickness = 3.0;


                Parent_x_1.Points.Add( new Point( _ScreenSizeX / 3, 0 ) );
                Parent_x_1.Points.Add( new Point( _ScreenSizeX / 3, _ScreenSizeY ) );

                Parent_x_2.Points.Add( new Point( _ScreenSizeX / 3 * 2, 0 ) );
                Parent_x_2.Points.Add( new Point( _ScreenSizeX / 3 * 2, _ScreenSizeY ) );

                Parent_y_1.Points.Add( new Point( 0, _ScreenSizeY / 3 ) );
                Parent_y_1.Points.Add( new Point( _ScreenSizeX, _ScreenSizeY / 3 ) );

                Parent_y_2.Points.Add( new Point( 0, _ScreenSizeY / 3 * 2 ) );
                Parent_y_2.Points.Add( new Point( _ScreenSizeX, _ScreenSizeY / 3 * 2 ) );

                draw.Children.Add( Parent_x_1 );
                draw.Children.Add( Parent_x_2 );
                draw.Children.Add( Parent_y_1 );
                draw.Children.Add( Parent_y_2 );

                _DevideWindow.DrawLine( draw );

                _DevideWindow.Width = _ScreenSizeX;
                _DevideWindow.Height = _ScreenSizeY;

                _DevideWindow.Left = 0;
                _DevideWindow.Top = 0;
                _DevideWindow.Show();
            }

            StartStoryboardMouse( Btn_Devide );
        }

        private void KeyEventShowOption ()
        {
            this.Dispatcher.BeginInvoke( new NoParamDelegate( ShowOptionDialog ) );
        }

        private void ShowOptionDialog ()
        {
            if (!CustomKeyboard._IsShow)
            {
                new CustomKeyboard(_MainWindow._ScreenKeyboard, _MainWindow._ScreenMouse, false).Show();
            }
        }
        
        #endregion


        #region 익스플로러 관련
        private void ExplorerBack()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Alt, WinApi.MacroKeys.Left);

            StartStoryboardMouse(Btn_ExplorerBack);
        }

        private void ExplorerNext()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Alt, WinApi.MacroKeys.Right);

            StartStoryboardMouse(Btn_ExplorerNext);
        }

        private void ExplorerClose()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Ctrl, WinApi.MacroKeys.W);

            StartStoryboardMouse(Btn_ExplorerClose);
        }

        private void ExplorerStart()
        {
            StartStoryboardMouse(Btn_ExplorerStart);

            System.Diagnostics.Process ps = new System.Diagnostics.Process();
            ps.StartInfo.FileName = "iexplore.exe";
            ps.Start();
        }

        private void ExplorerChangeSize()
        {
            StartStoryboardMouse(Btn_ExplorerChangeSize);

            IntPtr hwnd = WinApi.GetForegroundWindow();
            WinApi.RECT rt = new WinApi.RECT();
            WinApi.GetWindowRect(hwnd, out rt);

            int processID = 0;
            int threadID = WinApi.GetWindowThreadProcessId(hwnd, out processID);

            if (Process.GetProcessById(processID).ProcessName.Equals("iexplore"))
            {
                if ((Math.Abs(rt.Right) - Math.Abs(rt.Left)) == Screen.PrimaryScreen.Bounds.Width &&
                    (Math.Abs(rt.Bottom) - Math.Abs(rt.Top)) == Screen.PrimaryScreen.WorkingArea.Bottom)
                {
                    KeyEventCombineMacro(WinApi.MacroKeys.Win, WinApi.MacroKeys.Down);

                    Console.WriteLine("Max size");
                }

                else
                {
                    KeyEventCombineMacro(WinApi.MacroKeys.Win, WinApi.MacroKeys.Up);

                    Console.WriteLine("previous size");
                }
            }
        }

        private void ExplorerSizeUp()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Ctrl, WinApi.MacroKeys.NumPad_Plus);

            StartStoryboardMouse(Btn_ExplorerSizeUp);
        }

        private void ExplorerSizeDown()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Ctrl, WinApi.MacroKeys.NumPad_Minus);

            StartStoryboardMouse(Btn_ExplorerSizeDown);
        }

        private void ExplorerSizeOrigin()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Ctrl, WinApi.MacroKeys.Num_0);

            StartStoryboardMouse(Btn_ExplorerSizeOrigin);
        }

        private void ExplorerSetting()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Alt, WinApi.MacroKeys.X);

            StartStoryboardMouse(Btn_ExplorerSetting);
        }

        private void ExplorerAddress()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Ctrl, WinApi.MacroKeys.L);

            StartStoryboardMouse(Btn_ExplorerAddress);
        }

        private void ExplorerFavorites()
        {
            KeyEventCombineMacro(WinApi.MacroKeys.Alt, WinApi.MacroKeys.C);

            StartStoryboardMouse(Btn_ExplorerFavorite);
        }

        #endregion

        #region 마우스 블루투스

        public void WriteBluetoothSignal ( string data )
        {
            if ( !data.Equals( "-" ) )
            {
                _RecycleBuffer = data;
            }

            OnceSignal( data );

            if ( _BluetoothKeyEvents != null && _CustomSignal != null )
            {
                int index = -1;
                index = _CustomSignal.IndexOf( data );

                if ( index != -1 )
                {
                    _BluetoothKeyEvents[ index ]();
                }
            }
        }


        private delegate void RequestSignalDelegate ( Image str );
        private void OnceSignal ( string str )
        {

            this.Dispatcher.Invoke( new RequestSignalDelegate( RequestSignalWindow ), this.Dispatcher.Invoke( new GetSignalImageDelegate( GetSignalImage ), str ) );
        }

        private void RequestSignalWindow ( Image myImage )
        {
            if ( !MainWindow._IsSignal )
            {
                _MainWindow._Signal = new Signal();
                MainWindow._IsSignal = true;
            }

            _MainWindow._Signal.Left = 0;
            _MainWindow._Signal.Top = 0;
            _MainWindow._Signal.Content = myImage;

            if ( MainWindow._IsSignal )
                _MainWindow._Signal.Show();

        }


        private delegate Image GetSignalImageDelegate ( string path );

        private Image GetSignalImage ( string path )
        {
            System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();

            string tmpStr = path;
            tmpStr = tmpStr.Replace( ".", "dot" );
            tmpStr = tmpStr.Replace( "-", "dash" );

            // 소스 생성

            BitmapImage myBitmapImage = new BitmapImage();

            // BitmapImage.UriSource는 반드시 BeginInit/EndInit 블럭 내에 있어야 합니다.

            myBitmapImage.BeginInit();

            myBitmapImage.UriSource = new Uri( @"../../Image/dashdot/_" + tmpStr + ".png", UriKind.Relative );
            Console.WriteLine( "img : {0}", tmpStr );

            myBitmapImage.EndInit();




            // 이미지 소스 설정

            myImage.Source = myBitmapImage;
            return myImage;
        }
        //switch (data)
        //{
        //    case ".":
        //        MouseEventLeftClick();
        //        break;

        //    case "-":
        //        MouseEventRecycleCenter();
        //        break;

        //    case "..":
        //        MouseEventLeftDoubleClick();
        //        break;

        //    case ".-":
        //        MouseEventWheelDown();
        //        break;

        //    case "-.":
        //        MouseEventWheelUp();
        //        break;

        //    case "--":
        //        MouseEventRightClick();
        //        break;

        //    case "...":
        //        MouseEventLeftUp();
        //        break;

        //    case "..-":
        //        MouseEventUp();
        //        break;

        //    case ".-.":
        //        MouseEventLeft();
        //        break;

        //    case ".--":
        //        MouseEventRightUp();
        //        break;

        //    case "-..":
        //        MouseEventRight();
        //        break;

        //    case "-.-":
        //        MouseEventLeftDown();
        //        break;

        //    case "--.":
        //        MouseEventDown();
        //        break;

        //    case "---":
        //        MouseEventRightDown();
        //        break;

        //    case "....":
        //        MouseEventRightDoubleClick();
        //        break;

        //    case "...-":
        //        MouseEventOption();
        //        break;

        //    case "--..":
        //        MouseEventLeftRightClick();
        //        break;

        //    case "---.":
        //        MouseEventDevideDelegate();
        //        break;

        //    case "----":
        //        MouseEventDrag();
        //        break;

        //    case "--..-":
        //        MouseEventBackMain();
        //        break;

        //    case "-----":
        //        MouseEventToggleHideShow();
        //        break;
        //}
        // }

        private void MouseEventRecycleCenter ()
        {
            if ( _IsDevide )
            {
                MouseEventCenter();
            }
            else
            {
                MouseEventRecycle();
            }
        }

        private void MouseEventDevideDelegate ()
        {
            this.Dispatcher.BeginInvoke( new NoParamDelegate( MouseEventDevide ) );
        }

        public void WriteBluetoothAutoCompleteText ()
        {

        }


        #endregion


        #region 마우스 뷰어 확대축소


        

        /// <summary>
        /// 윈도우 크기 변경
        /// </summary>
        public void MouseEventChangeWindowSize()
        {
            this.Dispatcher.BeginInvoke(new NoParamDelegate(ChangeWindowSize));

           // StartStoryboardKeyboard(Btn_WindowSize);
        }

        /// <summary>
        /// 키보드 윈도우의 크기 변경, 각각 0.8배 1배 1.5배로 변경가능
        /// </summary>
        private void ChangeWindowSize()
        {

            if (_CurrentWindowSize == _WindowSizeType.Small)
            {
                _CurrentWindowSize = _WindowSizeType.Normal;
                this.Width = 585;
                this.Height = 290;
                ShowTextBoxNormal();
            }
            else if (_CurrentWindowSize == _WindowSizeType.Normal)
            {
                _CurrentWindowSize = _WindowSizeType.Large;
                this.Width = 878;
                this.Height = 435;
                ShowTextBoxLarge();
            }
            else
            {
                _CurrentWindowSize = _WindowSizeType.Small;
                this.Width = 468;
                this.Height = 232;
                ShowTextBoxSmall();
            }

            SetWindowLocation();

        }

        /// <summary>
        /// 윈도우 크기 0.8배 아이콘으로 변경
        /// </summary>
        private void ShowTextBoxSmall()
        {
            tbWindowSizeSmall.Visibility = Visibility.Visible;
            tbWindowSizeNormal.Visibility = Visibility.Hidden;
            tbWindowSizeLarge.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 윈도우 크기 1배 아이콘으로 변경
        /// </summary>
        private void ShowTextBoxNormal()
        {
            tbWindowSizeSmall.Visibility = Visibility.Hidden;
            tbWindowSizeNormal.Visibility = Visibility.Visible;
            tbWindowSizeLarge.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 윈도우 크기 1.5배 아이콘으로 변경
        /// </summary>
        private void ShowTextBoxLarge()
        {
            tbWindowSizeSmall.Visibility = Visibility.Hidden;
            tbWindowSizeNormal.Visibility = Visibility.Hidden;
            tbWindowSizeLarge.Visibility = Visibility.Visible;
        }


        #endregion

    }
}
